import { showModal } from "./uni.js";

// 检查小程序版本更新
export function wxCheckUpgrade() {
  console.log("== wx check upgrade ==");
  const manager = uni.getUpdateManager();
  manager.onUpdateReady = () => {
    showModal({
      title: "提示",
      content: "您的小程序新版本已准备好，是否重启更新?",
      success: ({ confirm }) => {
        if (confirm) {
          manager.applyUpdate();
        }
      },
    });
  };
}

// 发起微信登录
export function wxLogin() {
  return new Promise((resolve, reject) => {
    uni.showLoading({
      title: "正在登录",
    });
    uni.login({
      provider: "weixin",
      success: (res) => resolve(res),
      fail: (err) => reject(err),
      complete: () => uni.hideLoading(),
    });
  });
}

// 发起微信支付
export function wxPay(params) {
  return new Promise((resolve, reject) => {
    uni.showLoading({
      title: "正在支付",
    });
    uni.requestPayment({
      ...params,
      success: (res) => resolve(res),
      fail: (err) => reject(err),
    });
  });
}

// 开启退出提示
export function enableWxBackTip(msg = '返回后当前编辑内容不做保存') {
  wx.enableAlertBeforeUnload({
    message: msg
  })
}

// 禁止退出提示
export function disableWxBackTip() {
  wx.disableAlertBeforeUnload()
}